package net.aiform.service.security;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springside.modules.orm.hibernate.Page;
import org.springside.modules.orm.hibernate.SimpleHibernateTemplate;

import net.aiform.entity.org.Sysuser;
import net.aiform.entity.security.Authority;
import net.aiform.entity.security.Sysrole;
import net.aiform.service.ServiceException;

/**
 * 整个User模块内的业务逻辑Facade�?
 * 组合user,role,auth三�?的DAO.
 * DAO均由SimpleHibernateTemplate指定泛型生成.
 * 
 * @author calvin
 */
@Service
@Transactional
public class SecurityManager {

	private static String AUTH_HQL = "select count(u) from Sysuser u where u.loginName=? and u.password=?";


	private SimpleHibernateTemplate<Sysrole, String> roleDao;

	private SimpleHibernateTemplate<Authority, String> authDao;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		roleDao = new SimpleHibernateTemplate<Sysrole, String>(sessionFactory, Sysrole.class);
		authDao = new SimpleHibernateTemplate<Authority, String>(sessionFactory, Authority.class);
	}

	@Transactional(readOnly = true)
	public List<Sysrole> getAllRoles() {
		return roleDao.findAll();
	}

	@Transactional(readOnly = true)
	public Sysrole getRole(String id) {
		return roleDao.get(id);
	}

	public void saveRole(Sysrole role) {
		roleDao.save(role);
	}

	public void deleteRole(String id) {
		if ("1".equals(id))
			throw new ServiceException("不能删除超级管理员角�?");
		Sysrole role = roleDao.get(id);
		roleDao.delete(role);
	}

	@Transactional(readOnly = true)
	public List<Authority> getAllAuths() {
		return authDao.findAll();
	}

	@Transactional(readOnly = true)
	public Authority getAuthority(String id) {
		return authDao.get(id);
	}
}
